package org.dromara.system.controller;

import java.math.BigDecimal;
import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.*;
import org.dromara.system.domain.vo.AppProcurementPlanStaySendVo;
import org.dromara.system.service.IAppDriverDetailService;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppOrderVo;
import org.dromara.system.service.IAppOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订单
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/order")
public class AppOrderController extends BaseController {

    private final IAppOrderService appOrderService;
    private final IAppDriverDetailService appDriverDetailService;




    /**
     * 结算支付订单
     */
    @PostMapping("/orderPay")
    public R<?> orderPay(@RequestBody WxReq wxReq) {
        return R.ok(appOrderService.orderPay(wxReq));
    }


    /**
     * 微信支付回调
     */
    @SaIgnore
    @PostMapping("/wxCallBack")
    public R<?> wxCallBack(HttpServletRequest httpServletRequest) {
        return R.ok(appOrderService.wxCallBack(httpServletRequest));
    }





    /**
     * 仓库管理员 查看订单详情
     *
     * @param id 主键
     */
    @SaCheckPermission("system:order:query")
    @GetMapping("/{orderId}")
    public R<AppOrderVo> getInfoById(@NotNull(message = "主键不能为空")
                                 @PathVariable String id) {
        return R.ok(appOrderService.queryById(id));
    }



    /**
     * 下发供应单 订单进入待确认状态
     */
    @SaCheckPermission("system:driverDetail:list")
    @PostMapping("/sendSupplierOrder")
    @RepeatSubmit()
    public R<Void> sendSupplierOrder(AppOrderStaySendBo bo) {
        return appDriverDetailService.sendSupplierOrder(bo);
    }


    /**
     * 待下发和已下发采购订单列表
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/staySend")
    @RepeatSubmit()
    public R<List<AppProcurementPlanStaySendVo>> staySend(AppOrderStaySendBo bo) {
        if (bo.getOrderStatus()==null){
            return R.fail("请选择下发状态");
        }
        return appDriverDetailService.staySend(bo);
    }

    /**
     * 订单结算
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/settlement")
    @RepeatSubmit()
    public R<Void> settlement(AppDriverDetailBo bo) {
        return appDriverDetailService.settlement(bo);
    }



    /**
     * 质检提交
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/qualityInspectionSubmit")
    @RepeatSubmit()
    public R<Void> qualityInspectionSubmit(AppQualityInfoBo bo) {
        return appDriverDetailService.qualityInspectionSubmit(bo);
    }
    /**
     * 入库
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/warehouseEntry")
    @RepeatSubmit()
    public R<Void> warehouseEntry(AppOrderBo bo) {
        return appDriverDetailService.warehouseEntry(bo);
    }


    /**
     * 供应商 货品单价提交 变为已核算
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/setSinglePrice/{orderId}/{singlePrice}")
    @RepeatSubmit()
    public R<Void> setSinglePrice(@PathVariable("orderId") String orderId,@PathVariable("singlePrice") BigDecimal singlePrice) {
        return appDriverDetailService.setSinglePrice(orderId,singlePrice);
    }


    /**
     * 抢单
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/grabOrder")
    @RepeatSubmit()
    public R<Void> grabOrder(AppDriverDetailBo bo) {
        return appDriverDetailService.grabOrder(bo);
    }


    /**
     * 线上支付运输费和押车费 状态变为待支付 等仓库管理员入库后
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/onlinePay")
    @RepeatSubmit()
    public R<Void> onlinePay(AppDriverSureCarBo bo) {
        return appDriverDetailService.                                                                                                                                          onlinePay(bo);
    }

    /**
     * 确认承运
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/carrier")
    @RepeatSubmit()
    public R<Void> carrier(AppDriverSureCarBo bo) {
        return appDriverDetailService.carrier(bo);
    }


    /**
     * 确认发货 状态变为运输中
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/shipment")
    @RepeatSubmit()
    public R<Void> shipment(AppDriverSureSendBo bo) {
        return appDriverDetailService.shipment(bo);
    }



    /**
     * 确认送达 状态变为已送达
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/arrive")
    @RepeatSubmit()
    public R<Void> arrive(String orderId,String imgUrl) {
        return appDriverDetailService.arrive(orderId,imgUrl);
    }




    /**
     * 确认订单
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/sureOrder")
    @RepeatSubmit()
    public R<Void> sureOrder(AppOrderSureBo appOrderSureBo) {
        return appOrderService.sureOrder(appOrderSureBo);
    }



    /**
     * 查询订单列表
     */
    @SaCheckPermission("system:order:list")
    @GetMapping("/list")
    public TableDataInfo<AppOrderVo> list(AppOrderBo bo, PageQuery pageQuery) {
        return appOrderService.queryPageList(bo, pageQuery);
    }



    /**
     * 获取订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:order:query")
    @GetMapping("/detail/{id}")
    public R<AppOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(appOrderService.queryById(id));
    }

    /**
     * 新增订单
     */
    @SaCheckPermission("system:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppOrderBo bo) {
        return toAjax(appOrderService.insertByBo(bo));
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("system:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppOrderBo bo) {
        return toAjax(appOrderService.updateByBo(bo));
    }

    /**
     * 删除订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appOrderService.deleteWithValidByIds(List.of(ids), true));
    }
}
